import stackless, glob, re, sys, random, numpy, numpy.random, os, pickle, string, socket, urllib
from stacklesssocket import install
if __name__="__main__": install()
import socket as sock

class Singleton(object):
	def __new__(cls, *args, **kwargs):
		if '_singletoninstance' not in vars(cls):
			cls._singletoninstance = super(type, cls).__new__(cls, *args, **kwargs)
			return cls._singletoninstance
		


from xml.dom.minidom import parse, parseString, getDOMImplementation

import hostconfig, stackless, time
from generative import app

class labels(dict, Singleton):
		pass
class App_Client(Object):
	def __init__(self, labels):
		self.labels=labels
		self.newlabels=labels
		class app(generative.app):
			def __init__(self):
				generative.app.__init__(hostconfig.*host_args)
			def stream(self, stream):
				self.output.append(stream)
		return app
	def __call__(self):
		while stackless.getruncount() > 1:
			t = stackless.run(100000000)
			# If we got a tasklet back, it was the one that was interrupted.
			# we need to reinsert it for rescheduling.
			if t:
				t.insert()

	def __getitem__(self, labels):
			
class App_Server():
	def __init__(self):
		g=server.server(hostconfig.*server_args)

class App_User(App_Client):
	def __init__(self, name, labels={}, stream=None, wait=5*60):
		self=App_Client.__init__(labels)
		self.labels=stream
		self.cursor=(0,0)
		if stream==None:
			impl = getDOMImplementation()
			self.doc = impl.createDocument(None, name, None)
			self.name=name
		else:
			try:
				self.doc=parse(stream)
				self.stream=stream
			except:
				self.doc=parseString(stream)
			self.name=self.doc.documentElement.tagName
			self.root=self.pod("")
	def pod(self, operator):
		class _pod:
			app=self
			code=self.code
			op=operator
			def __init__(self, pos=None):
				if pos is None:
					self.pos=self.app.cursor
				else:
					self.pos=pos
				stackless.tasklet(autostore)()
				self.wait=1/10*(self.app.wait)
			def store(self, ancestor=None):
				parent=None
				if ancestor is None:
						parent=self.app.documentElement
				else:
						parent=ancestor
				while parent.childNodes!=None: parent=parent.getElementsByTagName("pod")[self.pos[1]]
				node=self.app.doc.createElement("doc")
				for i in self.__dict__.keys:
					node.setAttribute(i, self.__dict__[i])
				parent.addChild(node)
			def autostore(self, stream):
				stackless.schedule()
				while(1):
					if int(time.time()*10**int(-1*log10(self.wait)))%int(self.wait*10**int(-1*log10(self.wait)))==0: self.store()
			def compile(self):
				if self.op in ["=","-"]:
					return "<val>["+self.childNodes[0].compile()+self.op+self.childNodes[1].compile()+"]!;</val>"
				else:
					return "<val>["+self.op+self.childNodes[0]+"]!;</val>"
		return _pod
	def autosave(self, stream):
				stackless.schedule()
				while(1):
					if int(time.time()*10**int(-1*log10(self.wait)))%int(self.wait*10**int(-1*log10(self.wait)))==0: self.doc.writexml(stacklessfile(stream))		
	def compile(self):
		return self.root.compile()
	def label(self, labelname):
		self.labels[hostconfig.gen_tag()]=labelname

					
					

				